package com.maxmlr.music.controller;

import com.maxmlr.music.model.Result;
import com.maxmlr.music.model.Singer;
import com.maxmlr.music.model.Song;
import com.maxmlr.music.model.User;
import com.maxmlr.music.service.MainService;
import com.maxmlr.music.util.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;



/**
 * 主页控制层
 * 
 * @author maxiao
 * 
 */
@Controller
@RequestMapping("/main")
@Slf4j
public class MainController extends FathController {

	@Autowired
	private MainService mainService;

	/**
	 * 加载我的音乐
	 * 
	 * @return
	 */
	@RequestMapping("/tomainPage.do")
	public String tomainPage(Model model, HttpSession session) {
		User user = (User) session.getAttribute(USER);
		if (user != null) {
			model.addAttribute(USER, user);
		}
		return "page/mainPage";
	}

	/**
	 * 跳转top页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/totop.do")
	public String totop(Model model, HttpSession session) {
		User user = (User) session.getAttribute(USER);
		if (user != null) {
			model.addAttribute(USER, user);
		}
		return "page/sort";
	}

	/**
	 * 跳转special页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/tospecial.do")
	public String tospecial(Model model, HttpSession session) {
		User user = (User) session.getAttribute(USER);
		if (user != null) {
			model.addAttribute(USER, user);
		}
		return "page/special";
	}

	/**
	 * 跳转mv页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/tomv.do")
	public String tomv(Model model, HttpSession session) {
		User user = (User) session.getAttribute(USER);
		if (user != null) {
			model.addAttribute(USER, user);
		}
		return "page/mv";
	}

	/**
	 * 加载我的音乐
	 * 
	 * @return
	 */
	@RequestMapping("/tomymusic.do")
	public String tomymusic(HttpSession session, Model model) {
		User user = (User) session.getAttribute(USER);
		if (user != null) {
			model.addAttribute(USER, user);
		}
		return "page/myMusic";
	}

	/**
	 * 查看歌手信息
	 * 
	 * @param singer_id
	 * @return
	 */
	@RequestMapping("/tosinger.do")
	public String tosinger(Integer singer_id, Model model, HttpSession session) {
		Map<String, Object> map = mainService.fingSingerById(singer_id);
		User user = (User) session.getAttribute(USER);
		if (user != null) {
			model.addAttribute(USER, user);
		}
		if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
			model.addAttribute("singer", (Singer) map.get(RET));
			return "page/singer";
		}
		return "page/mainPage";
	}

	/**
	 * 查看歌曲信息
	 * 
	 * @param song_id
	 * @return
	 */
	@RequestMapping("/tosong.do")
	public String tosong(Integer song_id, Model model, HttpSession session) {
		Map<String, Object> map = mainService.findSongById(song_id);
		User user = (User) session.getAttribute(USER);
		if (user != null) {
			model.addAttribute(USER, user);
		}
		if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
			model.addAttribute("song", (Song) map.get(RET));
			return "page/myMusic";
		}
		return "page/singer";
	}

	/**
	 * 加载主页面歌手
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/addMainSinger.do")
	public Result addMainSinger() {
		Map<String, Object> map = mainService.findSinger();
		Result re = new Result();
		if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
			re.setData((List<Singer>) map.get(RET));
		}
		return re;
	}

	/**
	 * 加载歌手的歌曲
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/addSongBySinger.do")
	public Result addSongBySinger(Integer singer_id) {
		Map<String, Object> map = mainService.findSongBySingerId(singer_id);
		log.info("map"+map.toString());
		Result re = new Result();
		if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
			re.setData((List<Song>) map.get(RET));
		}
		return re;
	}

	/**
	 * 加载我的音乐 的歌
	 * 
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/addsong.do")
	public Result addsong(Integer page) {
		Map<String, Object> map = mainService.findSongNew(page);
		Result re = new Result();
		if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
			re.setData((List<Song>) map.get(RET));
		}
		return re;
	}

	/**
	 * 加载音乐的页数
	 * 
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addpageChange.do")
	public Result addpageChange() {
		Map<String, Object> map = mainService.findSongNumPage(SONGMP3);
		Result re = new Result();
		if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
			re.setData((Integer) map.get(RET));
		}
		return re;
	}

	/**
	 * 加载音乐的页数
	 * 
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findSongById.do")
	public Result findSongById(Integer song_id) {
		Map<String, Object> map = mainService.findSongById(song_id);
		Result re = new Result();
		if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
			re.setData((Song) map.get(RET));
		}
		return re;
	}
}
